﻿using Common;
using enterprise.Website.Core.Filter;
using FreeSql.Context;
using FreeSql.Model;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;


namespace enterprise.Website.Core.ApiControllers
{
    [Route("api/[controller]")]
    [ApiController]
    [LoginAuthorize]
    public class MessageInfoController : ControllerBase
    {
        private readonly SqlContext _sqlContext;
        public MessageInfoController(SqlContext sqlContext)
        {
            _sqlContext = sqlContext;
        }

        /// <summary>
        /// 获取留言列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<PageModel<Message_Info>> Get(int page = 1,int limit = 10)
        {
            return await _sqlContext.QueryPageListAsync<Message_Info>(page: page, limit: limit,orderBy:"Id desc");
        }

        /// <summary>
        /// 删除留言信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task<ApiResult> Delete(int id = 0)
        {
            var result = new ApiResult { Success = false, Message = "删除失败" };
            if(id>0)
            {
                var Count = await _sqlContext.DeleteAsync<Message_Info>(m => m.Id == id);
                if (Count > 0)
                {
                    result.Success = true;
                    result.Message = "删除成功";
                }
            }
            return result;
        }
    }
}
